﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cmsservice.Entity;
using cmsservice.DAL;
using cmsservice.Common;

namespace cmsservice.BLL
{
    public class F10GdyjBLL
    {
        F10GdyjDAL dal = new F10GdyjDAL();
        public List<F10_NewsComments> GetLst(string stock)
        {

            return dal.GetLst(stock);
        }
        public List<F10_IndustryNews> GetHYNewsLst(string code)
        {
           string  bkId = GetBkId(code);
           return dal.GetHYNewsLst(bkId);
        }
        public List<HYGL> GetGgInBk(string code, string blockCategory)
        {
            var lst = GetAll();
            return lst.Where(x => x.SecuCode == code && x.BlockCategory == blockCategory).ToList();
        }

        public List<HYGL> GetAll()
        {
            string key="F10GdyjBLL.GetAll";
            var lst = (List<HYGL>)Cache.Get(key);
            if (lst == null || lst.Count == 0)
            {
               
                lst = dal.GetGgInBk();
                Cache.Add(key, lst, DateTime.Now.AddMinutes(20));
            }
            return lst;
        }

        /// <summary>
        /// 占流通股比例
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public List<ZltgblEntity> GetZltgbl(string code)
        {
            List<ZltgblEntity> lst = new List<ZltgblEntity>();
            F10GdyjDAL dal = new F10GdyjDAL();
            lst = dal.GetZltgbl(code);
            return lst;
        }


        /// <summary>
        /// 潜在分红能力分析
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public FhrzEntity GetFhrz(string code)
        {
            FhrzEntity obj = new FhrzEntity();
            F10GdyjDAL dal = new F10GdyjDAL();
            List<FhrzEntity> lst = new List<FhrzEntity>();
            lst = dal.GetFhrz();
            if (lst != null)
            {
                lst = lst.Where(x => x.SecuCode == code).ToList();
                if (lst != null && lst.Count > 0)
                {
                    obj = lst[0];
                }
            }
            return obj;
        }

        /// <summary>
        /// 财务分析-盈利能力
        /// </summary>
        /// <param name="code"></param>
        /// <param name="bkcode"></param>
        /// <returns></returns>
        public List<YlnlLstEntity> GetCwYlnl(string code, string bkcode)
        {
            List<YlnlLstEntity> lst = new List<YlnlLstEntity>();
            F10GdyjDAL dal = new F10GdyjDAL();
            var secu = GetObjectByStock(code);
            bkcode = GetBkId(code);
            lst = dal.GetCwYlnl(code, bkcode);
            return lst;
        }

        public static string GetBkId(string stock)
        {
            string key = "Emoney.F10.BLL.Em_Block_StockBLL.GetOne." + stock;
            string str = Cache.Get<string>(key);
            if (!string.IsNullOrEmpty(str))
            {
                return str;
            }
            else
            {
                F10GdyjDAL dal = new F10GdyjDAL();
                var c = dal.GetOne(stock);
                if (c != null)
                {
                    Cache.Add(key, c.BlockCode, DateTime.Now.AddHours(6));
                    return c.BlockCode;
                }
                else
                {
                    return "";
                }
            }
        }
        
        public static Security GetObjectByStock(string stock)
        {
            string key = "SecurityBLL.GetObjectByStock";
            var dic = Cache.Get<Dictionary<string, Security>>(key);
            if (dic != null && dic.Count > 0)
            {
                if (dic.ContainsKey(stock))
                {
                    return dic[stock];
                }
                else
                {
                    return null;
                }
            }
            SecurityDAL dal = new SecurityDAL();
            var lst = dal.GetAllStocks();
            dic = new Dictionary<string, Security>();
            foreach (var c in lst)
            {
                if (!dic.ContainsKey(c.SecuCode))
                {
                    dic.Add(c.SecuCode, c);
                }
            }
            Cache.Add(key, dic, DateTime.Now.AddMinutes(10));
            if (dic.ContainsKey(stock))
            {
                return dic[stock];
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 财务分析-收益质量
        /// </summary>
        /// <param name="code"></param>
        /// <param name="bkcode"></param>
        /// <returns></returns>
        public List<SyzlLstEntity> GetCwSyzl(string code, string bkcode)
        {
            List<SyzlLstEntity> lst = new List<SyzlLstEntity>();
            F10GdyjDAL dal = new F10GdyjDAL();

            bkcode = GetBkId(code);
            lst = dal.GetCwSyzl(code, bkcode);
            return lst;
        }

        /// <summary>
        /// 财务分析-现金流量
        /// </summary>
        /// <param name="code"></param>
        /// <param name="bkcode"></param>
        /// <returns></returns>
        public List<XjllLstEntity> GetCwXjll(string code, string bkcode)
        {
            List<XjllLstEntity> lst = new List<XjllLstEntity>();
            F10GdyjDAL dal = new F10GdyjDAL();
            bkcode = GetBkId(code);
            lst = dal.GetCwXjll(code, bkcode);
            return lst;
        }

        /// <summary>
        /// 财务分析-资本结构
        /// </summary>
        /// <param name="code"></param>
        /// <param name="bkcode"></param>
        /// <returns></returns>
        public List<ZbjgLstEntity> GetCwZbjg(string code, string bkcode)
        {
            List<ZbjgLstEntity> lst = new List<ZbjgLstEntity>();
            F10GdyjDAL dal = new F10GdyjDAL();
            bkcode = GetBkId(code);
            lst = dal.GetCwZbjg(code, bkcode);
            return lst;
        }

        /// <summary>
        /// 财务分析-偿债能力
        /// </summary>
        /// <param name="code"></param>
        /// <param name="bkcode"></param>
        /// <returns></returns>
        public List<CznlLstEntity> GetCwCznl(string code, string bkcode)
        {
            List<CznlLstEntity> lst = new List<CznlLstEntity>();
            F10GdyjDAL dal = new F10GdyjDAL();
            bkcode = GetBkId(code);
            lst = dal.GetCwCznl(code, bkcode);
            return lst;
        }

        /// <summary>
        /// 财务分析-营运能力
        /// </summary>
        /// <param name="code"></param>
        /// <param name="bkcode"></param>
        /// <returns></returns>
        public List<YynlLstEntity> GetCwYynl(string code, string bkcode)
        {
            List<YynlLstEntity> lst = new List<YynlLstEntity>();
            F10GdyjDAL dal = new F10GdyjDAL();
            bkcode = GetBkId(code);
            lst = dal.GetCwYynl(code, bkcode);
            return lst;
        }

        /// <summary>
        /// 财务分析-成长能力
        /// </summary>
        /// <param name="code"></param>
        /// <param name="bkcode"></param>
        /// <returns></returns>
        public List<CzhnlLstEntity> GetCwCzhnl(string code, string bkcode)
        {
            List<CzhnlLstEntity> lst = new List<CzhnlLstEntity>();
            F10GdyjDAL dal = new F10GdyjDAL();
            bkcode = GetBkId(code);
            lst = dal.GetCwCzhnl(code, bkcode);
            return lst;
        }

    }
    [Serializable]
    public class EM_F10_Profitability
    {
        /// <summary>
        ///机构编号
        /// </summary>
        public string OrgID
        {
            get;
            set;
        }

        /// <summary>
        ///报告期
        /// </summary>
        public DateTime ReportPeriod
        {
            get;
            set;
        }

        /// <summary>
        ///净资产收益率
        /// </summary>
        public decimal? NetAssetsIncomeRate
        {
            get;
            set;
        }

        /// <summary>
        ///总资产净利率
        /// </summary>
        public decimal? JROA
        {
            get;
            set;
        }

        /// <summary>
        ///销售毛利率
        /// </summary>
        public decimal? GrossProfitSalesRatio
        {
            get;
            set;
        }

        /// <summary>
        ///销售净利率
        /// </summary>
        public decimal? SalesNetInterestRate
        {
            get;
            set;
        }

        /// <summary>
        ///销售期间费用率
        /// </summary>
        public decimal? DuringSalesRate
        {
            get;
            set;
        }

        /// <summary>
        ///
        /// </summary>
        public decimal? ProfitabilityIndex
        {
            get;
            set;
        }

    }
}
